const titles=[{
  title:'1+1等于几',
  answer:['A'],
  options:[{
    code:'A',
    option:'2'
  },{
    code:'B',
    option:'3'
  },{
    code:'C',
    option:'4'
  },{
    code:'D',
    option:'5'
  }]
},
{
title:'10+10等于几',
  answer:['A'],
  options:[{
    code:'A',
    option:'20'
  },{
    code:'B',
    option:'30'
  },{
    code:'C',
    option:'40'
  },{
    code:'D',
    option:'50'
  }]
},
{
title:'国家安全日是什么时候',
  answer:['B'],
  options:[{
    code:'A',
    option:'4月14日'
  },{
    code:'B',
    option:'4月15日'
  },{
    code:'C',
    option:'4月16日'
  },{
    code:'D',
    option:'4月17日'
  }]
}
]
let errorOptions=[]
const app=getApp()
Page({
  data:{
    current:1,
    isSelect:false,
    total:0,
    percent:0,
    subject:null,
    userSelect:'',
    userScore:0,
    totalScore:-1,
    totalError:0
  },
  onLoad(){
    let subject=titles[0]
    console.log('subject',subject)
    this.setData({
      subject,
      total:titles.length
    })
  },
  radioChange(e){
    this.setData({
      userSelect:e.detail.value
    })
  },
  submit(){
    let userSelect=this.data.userSelect
    if(!userSelect){
      wx.showToast({
        icon:'none',
        title: '请做选择',
      })
      return
    }
   
    let currentNum=this.data.current
    this.setData({
      percent:((currentNum)/titles.length * 100).toFixed(1)
    })

    console.log('用户选择了',userSelect)
    console.log('正确答案',this.data.subject.answer)
    if(this,this.data.subject.answer.indexOf(userSelect)>-1){
      console.log('用户答对了第'+currentNum+"题")
      this.setData({
        userScore:this.data.userScore + 1  
      })

    }else{
      let subjectNow=this.data.subject
      subjectNow.userSelect=userSelect
      console.log('错题',subjectNow)
      errorOptions.push(subjectNow);
    }
    console.log('用户答对了'+this.data.userScore+'题')

    if(currentNum+1>titles.length){
      let totalScore=(this.data.userScore/titles.length * 100).toFixed(1)
      console.log('用户得分',totalScore)
      console.log('用户错题',errorOptions)
      let totalError=errorOptions.length;
      this.setData({
        totalScore:totalScore,
        totalError:totalError
      })
      wx.showToast({
        icon:'none',
        title: '已经是最后一题了',
      })
      return
    }
    let subject=titles[currentNum]
    this.setData({
      userSelect:'',
      subject:subject,
      current:currentNum+1,
      isSelect:false
      
    })
  },
  seeError(){
    console.log('查看错题')
    
    //app.globalData.globalErrorOptions=errorOptions

    wx.setStorageSync('cuoti', errorOptions)
    // wx.navigateTo({
    //   url: '/pages/errorOptions/errorOPtions',
    // })
    wx.switchTab({
      url: '/pages/errorOptions/errorOptions',
    })
  },
  tuichu(){
    wx.navigateTo({
      url: '../home/home',
    })
  }
})